package com.xiaoxie.boot.controller;


import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoxie.boot.bean.Notice;
import com.xiaoxie.boot.service.NoticeService;
import com.xiaoxie.boot.utils.DataInfo;
import com.xiaoxie.boot.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author xiaoxie
 * @create 2022-02-15 1:22
 */
@Api(tags = "公告API")
@RestController
@Slf4j
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 查询所有公告信息
     *
     * @param notice
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryAllNotice")
    @ApiOperation(value = "分页查询", notes = "查询所有公告信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryAllNotice(Notice notice, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15") Integer limit) {
        PageInfo<Notice> noticePageInfo = noticeService.selectAllNotice(notice, page, limit);
        return DataInfo.ok("成功", noticePageInfo.getTotal(), noticePageInfo.getList());
    }

    /**
     * 添加公告时检查公告标题是否存在
     *
     * @param title
     * @return
     */
    @GetMapping("/titleExist/{title}")
    @ApiOperation(value = "去重", notes = "添加公告时检查公告标题是否存在")
    @ApiOperationSupport(author = "小谢")
    public DataInfo titleExist(@PathVariable("title") String title) {
        Notice notice = noticeService.selectByTitle(title);
        if (notice == null) {
            return DataInfo.ok();
        } else {
            return DataInfo.fail("公告标题已存在!");
        }
    }

    /**
     * 公告添加
     *
     * @param notice
     * @param request
     * @return
     */
    @PostMapping("/addNotice")
    @ApiOperation(value = "新增", notes = "公告添加")
    @ApiOperationSupport(author = "小谢")
    public DataInfo addNotice(@RequestBody Notice notice, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "添加了公告：" + notice.getTitle());
        notice.setCreator(name);
        notice.setCreateTime(DateUtil.getTimestamp());
        int i = noticeService.insertSelective(notice);
        if (i == 1) {
            log.info("添加成功");
            return DataInfo.ok();
        } else {
            log.info("添加失败");
            return DataInfo.fail("公告添加失败!");
        }
    }

    /**
     * 修改公告信息
     *
     * @param notice
     * @param request
     * @return
     */
    @PutMapping("/editNotice")
    @ApiOperation(value = "修改", notes = "修改公告信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo editNotice(@RequestBody Notice notice, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "修改了公告：" + notice.getTitle() + "的信息");
        int i = noticeService.updateByPrimaryKeySelective(notice);
        if (i == 1) {
            log.info("修改成功");
            return DataInfo.ok();
        } else {
            log.info("修改失败");
            return DataInfo.fail("公告修改失败!");
        }
    }

    /**
     * 删除公告信息
     *
     * @param ids
     * @param request
     * @return
     */
    @DeleteMapping("/deleteNotice")
    @ApiOperation(value = "删除", notes = "删除公告信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo deleteNotice(String ids, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        List<String> list = Arrays.asList(ids.split(","));
        log.info(name + "试图删除公告信息");
        final int i = noticeService.deleteNoticeByIds(list);
        if (i >= 1) {
            log.info("删除了：" + i + "条信息");
            return DataInfo.ok();
        } else {
            log.info("删除失败");
            return DataInfo.fail("删除失败");
        }
    }
}
